from PyQt5 import QtWidgets
from util import popup_window_ui
import sys
from PyQt5.QtWidgets import QTableWidget, QHeaderView
from util.api_wapper import kuai_api, yemai_api, YeMaiAPI, yuan_api
from util.sqlite_db import *


class PopupWindow(popup_window_ui.Ui_Object):

    def __init__(self, popup_window, previous_window, phonenumber):
        super().setupUi(popup_window)  # 调用父类的setupUI函数
        self.popup_window = popup_window
        self.previous_window = previous_window
        self.label.setText(
            f"<html><head/><body><p align=\"center\"><span style=\" font-size:28pt; font-weight:600; color:#ff0000;\">{phonenumber}</span></p></body></html>"
        )
        self.mobile = phonenumber
        self.pushButton.clicked.connect(self.login)
        self.lineEdit.setText('')

    def login(self):
        sms_letter = self.lineEdit.text()
        succ, data = yemai_api.api_login(self.mobile, sms_letter)
        if succ:
            self.previous_window.set_log_out('成功登录:{}'.format(self.mobile))
            self.previous_window.set_success_out('成功登录:{}'.format(self.mobile))
            # 入库
            succ, member = yemai_api.api_huiyuan_list(data['accessToken'])
            update_user_login_token(self.mobile, data['accessToken'],
                                    data['user_id'],
                                    '是' if member['is_autonym'] else '否',
                                    member['point'])
            create_user_login_success(self.mobile, data['accessToken'],
                                      data['user_id'],
                                      '是' if member['is_autonym'] else '否',
                                      member['point'])
        else:
            self.previous_window.set_log_out('{}登录失败,原因:{}!!'.format(
                self.mobile, data))
        self.previous_window.user_list('YES')
        self.popup_window.close()


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    popup_window = QtWidgets.QMainWindow()
    popup_window_ui = PopupWindow(popup_window, '1234567')
    popup_window.setFixedSize(popup_window.width(), popup_window.height())
    popup_window.show()
    sys.exit(app.exec_())
